<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/4/15
 * Time: 15:02
 */
class PushmsgModel extends PublicModel{
	private $tid = array(
		'confirm'       => 'EN87o9S8ZiXaTwTyOnmXZKVCOFuK2NU35NiOKj6eats',       // 订单确认提醒模板ID
	    'survey'        => 'JGoCqfR7sHyxAct8kvKq98Y1cutD3ByfU2Rc9kUxrTI',       // 尽调员尽职调查提醒模板ID
	    'createShop'    => 'A7n7Mgkj5GbNBcTPWcm-6R1yBphw6Mnjdd9_n_W6ax0',       // 创建商铺成功提醒模板ID
	);

	public function message($weixin, $openid, $msg){
		return $this->pushMsg('message', $weixin, 0, $openid, $msg);
	}

	public function template($param){
		switch($param['type']){
			case 'confirm':
				return $this->confirm($param);
			case 'register':
				break;
			case 'survey':
				return $this->survey($param);
			case 'createShop':
				return $this->createShop($param);
			case 'subscribe':
				break;
			case 'orderPass':
				break;
			case 'orderReject':
				break;
			case 'orderOutime':
				break;
		}
	}

	private function pushMsg($type, $param, $tid, $openid, $data, $url=null){
		Vendor('Weixin');
		$param = defaultParam($param, array('WKSHOP', 'WAKA'), 'WKSHOP');
		$weixin = new Weixin($param);
		switch($type){
			case 'template':
				return $weixin->send_template_message($openid, $this->tid[$tid], $data, $url);
			case 'message':
				return $weixin->send_message($openid, $data);
			case 'news':
				return $weixin->push_message($openid, $data, 'news');
		}
	}

	/**
	 * 发送订单确认提醒
	 * @param $param['type']    APP端POST方式传递过来的推送类型, 此处为: confirm
	 * @param $param['id']      APP端POST方式传递过来的订单编号,或订单表主键
	 * @param $param['uid']     APP端POST方式传递过来的微信端用户uid值
	 * @param $param['money']   APP端POST方式传递过来的订单金额
	 *
	 * @return string
	 */
	public function confirm($param){
		$shop = M('WangkaBindView')->where('uid='.$param['uid'])->find();
		$url = APP_URL.U('Business/onePage', array(
				'type' => $param['type'],
				'id'   => $param['id'],
				'uid'  => $param['uid'],
			));
		$order = D('Business')->createOrder($param['id'], $param['uid']);
		if($order['errcode'] == '0'){
			$data = array(
				'first'     => array('value' => '尊敬的汪卡签约商户 您好，您有一笔消费订单等待您的确认: ', 'color' => '#173177'),
				'keyword1'  => array('value' => $shop['title']),
				'keyword2'  => array('value' => $param['mobile'], 'color' => '#173177'),
				'keyword3'  => array('value' => substr_replace($param['id'], '****', 8, 18)),
				'keyword4'  => array('value' => '汪卡APP支付', 'color' => '#173177'),
				'keyword5'  => array('value' => '应付￥'.$order['data']['sum_money'].'元，折扣￥'.$order['data']['ticket_money'].'元，实付￥'.$param['money'].'元'),
				'remark'    => array('value' => '请点击详情进行交易确认! 提醒: 如果6小时内订单未做确认, 此笔订单将被取消! ', 'color' => '#ef2323')
			);
			if($shop['openid'] != $shop['boss_openid'])
				$this->pushMsg('template', 'WKSHOP', 'confirm', $shop['boss_openid'], $data, $url);
			return $this->pushMsg('template', 'WKSHOP', 'confirm', $shop['openid'], $data, $url);
		}else{
			return $order;
		}
	}

	/**
	 * 发送尽调员尽职调查提醒
	 * @param   $param['type']      APP端POST方式传递过来的推送类型, 此处为: survey
	 * @param   $param['id']        APP端POST方式传递过来的需尽调用户的id
	 * @param   $param['uid']       APP端POST方式传递过来的微信端尽调员用户uid值
	 *
	 * @return string
	 *
	 */
	public function survey($param){
		$target = '/Public/Upload/Customer';
		$openid = M('WangkaUser')->where('uid='.$param['uid'])->getField('openid');
		$realnameTable = M('WakaRealname');
		$remoteServer = M('RealnameCustomerView','t_','mysql://weixin_user:weixin_HYCT0871@120.25.67.227/stages');
		$user = $remoteServer->where('uuid="'.$param['id'].'"')->find();
		$post['dataline'] = time();
		foreach($user AS $key => $val){
			if(in_array($key, array('headimgurl', 'idcard_url_1', 'idcard_url_2', 'yikatong_url', 'student_card_url', 'other_url'))){
				if(!is_null($val)){
					$res = $this->getMedia($val, $target);
					if($res['type'] == 'error')
						$post[$key] = null;
					else
						$post[$key] = 'http://sta.mywoka.com'.$res['url'];
				}
			}elseif($key != 'id'){
				$post[$key] = $val;
			}
		}
		if($realnameTable->where('uuid="'.$post['uuid'].'"')->count() == 0){
			$rec = $this->curd(array(
				'model' => $realnameTable,
				'type'  => 'add',
				'data'  => $post,
			));
		}else{
			$rec = $this->curd(array(
				'model' => $realnameTable,
				'type'  => 'save',
				'where' => 'uuid="'.$post['uuid'].'"',
				'data'  => $post,
			));
		}
		//log::write(json_encode_plus($rec), 'Return');
		if($rec['errcode'] == '0'){
			$url = APP_URL.U('Customer/onePage', array('type' => $param['type'], 'rid' => $rec['pk']));
			$content = '请安排时间对用户姓名: '.$user['name'].'，联系电话: '.$user['telephone'].'，所在学校: '.$user['schoolname'].'，所住宿舍: '.$user['dormitory'].' 的用户进行尽职调查工作! ';
			$data = array(
				'first'     => array(
					'value' => '尊敬的尽调员 您好，有一位用户选择您为TA做实名认证的尽职调查工作，祝您工作愉快! ',
					'color' => '#173177'
				),
				'noticeType'  => array('value' => '工作流程'),
				'bizType'  => array('value' => '尽职调查', 'color' => '#173177'),
				'message'  => array('value' => $content),
				'remark'    => array(
					'value' => '当您与用户见面时, 请点击详情进入该用户的尽职调查工作页面完成此次尽职调查工作! ',
					'color' => '#ef2323'
				)
			);
			return $this->pushMsg('template', 'WAKA', 'survey', $openid, $data, $url);
		}else{
			return $rec;
		}
	}

	/**
	 * 微信用户成功创建店铺发送消息提醒
	 * @param $param['openid']      消息提醒接收者OPENID
	 * @param $param['title']       所创建的店铺名称
	 * @param $param['category']    所创建的店铺分类或者所属商圈
	 * @param $param['mobile']      创建店铺者的手机号码
	 * @param $param['password']    所创建店铺的员工密码
	 *
	 * @return mixed
	 */
	public function createShop($param){
		$data = array(
			'first'     => array(
				'value' => '亲爱的用户 您好! 您已经成功创建商铺,您的微信号也同时绑定成该商铺的老板账户,您可以通过微信管理您的员工: ',
				'color' => '#173177'
			),
			'keyword1'  => array('value' => $param['title']),
			'keyword2'  => array('value' => $param['category'],'color' => '#ef2323'),
			'keyword3'  => array('value' => $param['mobile']),
			'keyword4'  => array('value' => $param['password']),
			'remark'    => array('value' => '您还需要将您的商铺的认证串码告诉您的员工,方便他们通过微信绑定成为您商铺的员工！点击详情查看您的商铺二维码...', 'color' => '#ef2323'),
		);
		$url = APP_URL.U('Business/Index', array('an' => 'qrcode'));
		return $this->pushMsg('template', 'WKSHOP', 'createShop', $param['openid'], $data, $url);
	}

}